package com.example.pungolo.journal.view;

import java.util.Calendar;
import java.util.Locale;

import com.example.pungolo.journal.R;
import com.example.pungolo.journal.U;
import com.example.pungolo.journal.model.DiaryIO;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MonthView extends LinearLayout {
  private Calendar currentCalendar;
  private int firstDayOfTheWeek;
  private OnClickListener onDayClickedListener;
  private int clickedDay;

  public MonthView(Context context) {
    super(context);
    init();
  }

  public MonthView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  @SuppressLint("NewApi")
  public MonthView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  private void init() {
    if (this.isInEditMode())
      return;
    View.inflate(getContext(), R.layout.month_view, this);
    ImageButton prev = (ImageButton) findViewById(R.id.mv_button_previous_month);
    ImageButton next = (ImageButton) findViewById(R.id.mv_button_next_month);
    TableLayout calendarTable = (TableLayout) findViewById(R.id.mv_calendar_table);

    prev.setOnClickListener(new OnClickListener() { public void onClick(View v) { prevMonth(); } });
    next.setOnClickListener(new OnClickListener() { public void onClick(View v) { nextMonth(); } });
    
    Calendar c = Calendar.getInstance();
    firstDayOfTheWeek = c.getFirstDayOfWeek();
    Locale locale = Locale.getDefault();
    
    c.set(Calendar.DAY_OF_MONTH, 1);
    int o = c.get(Calendar.DAY_OF_WEEK) - firstDayOfTheWeek;
    
    TableRow firstRow = (TableRow) calendarTable.getChildAt(0);
    for (int dow = 0; dow < 7; dow++) {
      TextView txt = (TextView) firstRow.getChildAt((o+dow)%7);
      txt.setText(String.format(locale, "%tA", c));
      c.add(Calendar.DAY_OF_WEEK, 1);
    }

    OnClickListener eachButton = new OnClickListener() { public void onClick(View v) { dayClicked((Integer)v.getTag()); } };
    for (int row = 0; row < 6; row++) {
      TableRow tlr = (TableRow) calendarTable.getChildAt(row + 1);
      for (int column = 0; column < 7; column++) {
        Button btn = (Button) tlr.getChildAt(column);
        btn.setOnClickListener(eachButton);
      }
    }

    setMonth(Calendar.getInstance());
  }
  
  public void setMonth(Calendar calendar) {
    this.currentCalendar = calendar;
    updateMonth();
  }
  
  public void nextMonth() {
    currentCalendar.add(Calendar.MONTH, 1);
    updateMonth();
  }
  
  public void prevMonth() {
    currentCalendar.add(Calendar.MONTH, -1);
    updateMonth();
  }

  private void updateMonth() {
    Calendar calendar = this.currentCalendar;
    TableLayout calendarTable = (TableLayout) findViewById(R.id.mv_calendar_table);
    TextView monthName = (TextView) findViewById(R.id.mv_month_name);
    Locale locale = Locale.getDefault();
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    boolean[] pages = new boolean[31];
    int firstDay = U.dayFromDate(calendar.getTime());
    DiaryIO.getDatabase().getDaysFromMonth(firstDay, pages);
    
    monthName.setText(String.format(locale, "%tB\n%tY", calendar, calendar));
    int count = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    int o = calendar.get(Calendar.DAY_OF_WEEK) - firstDayOfTheWeek;
    
    for (int row = 0; row < 6; row++) {
      TableRow tlr = (TableRow) calendarTable.getChildAt(row + 1);
      for (int column = 0; column < 7; column++) {
        Button btn = (Button) tlr.getChildAt(column);
        
        int i = row * 7 + column;
        if (i >= o && i < count + o) {
          btn.setEnabled(pages[i - o]);
          btn.setText((i - o + 1) + "");
          btn.setTag(i - o + 1);
          btn.setVisibility(View.VISIBLE);
        } else {
          btn.setEnabled(false);
          btn.setText("");
          btn.setVisibility(View.INVISIBLE);
        }
      }
    }
  }
  
  public void setOnDayClickedListener(OnClickListener listener) {
    this.onDayClickedListener = listener;
  }
  
  public int getClickedDay() {
    return clickedDay;
  }
  
  private void dayClicked(int day) {
    currentCalendar.set(Calendar.DAY_OF_MONTH, day);
    clickedDay = U.dayFromDate(currentCalendar.getTime());
    if (this.onDayClickedListener != null)
      this.onDayClickedListener.onClick(this);
  }

  public void refreh() {
    updateMonth();
  }
}
